<?php

namespace App\Http\Controllers\College\Web;

use App\Http\Controllers\BaseController;
use App\Models\College\CollegeTrainingPlanModel;
use App\Service\BaseService;
use App\Service\College\TrainingPlansService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class TrainingPlansController extends BaseController
{
    /**
     * 培训计划列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum = (int)$request->get('pageNum', 1);
        $name = (string)$request->get('name', '');
        $campus = (string)$request->get('campus', '');
        $planStartTime = (string)$request->get('planStartTime', '');
        $planEndTime = (string)$request->get('planEndTime', '');
        $status = (string)$request->get('status', '');
        $data = TrainingPlansService::getTrainingPlansList($pageSize, $pageNum, $name, $campus, $planStartTime, $planEndTime, $status);
        return $this->success($data);
    }

    /**
     * 添加培训计划
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws \Exception
     */
    public function add(Request $request): JsonResponse
    {
        $this->validate($request, [
            'name' => 'required|string|max:64',
            'type' => 'required|integer|between:1,2',
            'campus' => 'required|integer|between:1,'.count(CollegeTrainingPlanModel::TRAINING_PLANS_CAMPUS_MAPPING),
            'planStartTime' => 'required|string',
            'planEndTime' => 'required|string',
        ]);
        TrainingPlansService::addTrainingPlans($request);
        return $this->success();
    }


    /**
     *
     * 修改培训计划
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws \Exception
     */
    public function update(Request $request): JsonResponse
    {
        $this->validate($request, [
            'name' => 'required|string|max:64',
            'type' => 'required|integer|between:1,2',
            'campus' => 'required|integer|between:1,'.count(CollegeTrainingPlanModel::TRAINING_PLANS_CAMPUS_MAPPING),
            'planStartTime' => 'required|string|min:0',
            'planEndTime' => 'required|string|min:0',
        ]);
        if (empty($request->id)) {
            return BaseService::throwErr("id不能为空");
        }
        TrainingPlansService::updateTrainingPlans($request);
        return $this->success();
    }

    /**
     * get info
     * @param Request $request
     * @return JsonResponse
     */
    public function info(Request $request)
    {
        if (empty($request->id)) {
            return BaseService::throwErr("id不能为空");
        }
        $data = TrainingPlansService::getTrainingPlansInfo($request);
        return $this->success($data);
    }

    /**
     * 删除培训计划
     * @param Request $request
     * @return JsonResponse
     * @throws \Exception
     */
    public function delete(Request $request)
    {
        if (empty($request->id)) {
            return BaseService::throwErr("id不能为空");
        }
        TrainingPlansService::deleteTrainingPlans($request);
        return $this->success();
    }


    /**
     * 批量添加培训计划
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws \Exception
     */
    public function addBatch(Request $request): JsonResponse
    {
        $this->validate($request, [
            "campus" => "required|array",
            'count' => 'required|integer|min:1',
            'years' => 'required|integer|min:2022',
        ]);
        foreach ($request->campus as $v) {
            if (!in_array($v, array_keys(CollegeTrainingPlanModel::TRAINING_PLANS_CAMPUS_MAPPING))) {
                return BaseService::throwErr("campus参数不合法");
            }
        }
        TrainingPlansService::addBatchTrainingPlans($request);
        return $this->success();
    }
}
